﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Activities.Presentation;
using System.Activities.Presentation.Toolbox;
using System.Activities.Statements;
using System.Activities.Presentation.View;
using System.Activities.Core.Presentation;
namespace wfDesigner
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class FlowDesign : Window
    {
        private WorkflowDesigner wd = null;
        private string filename = "WFTemplate.xaml";

        private void AddDesigner()
        {
            this.wd = new WorkflowDesigner();//加载设计区域
        }

        private ToolboxControl GetTBC()
        {
            ToolboxControl rtn = new ToolboxControl();
            ToolboxCategory tbc = new ToolboxCategory("流程节点");
            ToolboxItemWrapper tiw = new ToolboxItemWrapper(typeof(Flowchart),"流程框架");
            tbc.Add(tiw);
            tiw = new ToolboxItemWrapper(typeof(FlowDecision),"判断");//system.flowchart
            tbc.Add(tiw);
            rtn.Categories.Add(tbc);
            return rtn;
        }

        private void AddToolBox()
        {
            ToolboxControl tbc = this.GetTBC();
            this.toolboxPanel.Content = tbc;
        }

        private void AddPropertyiesInspertor()
        {
            if (wd == null)
                return;
            this.propertyPanel.Content = this.wd.PropertyInspectorView;
        }

        private void LoadWorkflowFromFile(string filePath)
        {
            this.designerPanel.Content = null;
            this.propertyPanel.Content = null;

            wd = new WorkflowDesigner();
            wd.Load(filePath);

            DesignerView dv = wd.Context.Services.GetService<DesignerView>();
            dv.WorkflowShellBarItemVisibility = ShellBarItemVisibility.All;

            this.designerPanel.Content = wd.View;
            this.propertyPanel.Content = wd.PropertyInspectorView;
        }

        public FlowDesign()
        {
            InitializeComponent();
            (new DesignerMetadata()).Register();//要求注册
            this.AddDesigner();//加载WF设计
            this.AddToolBox();
            this.AddPropertyiesInspertor();
        }

        private void CreateWFMenuItem_Click(object sender, RoutedEventArgs e)
        {
            LoadWorkflowFromFile(this.filename);
        }

      

    
    }
}
